package com.zzyl.service.impl;



import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author Fan Jiajun
 * @Date 2025年5月10日
 */

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingLevelVo> findAll() {
        List<NursingLevel> nursingLevels = nursingLevelMapper.selectAll();
        List<NursingLevelVo> nursingLevelVos = nursingLevels.stream().map(pojo -> {
            NursingLevelVo nursingLevelVo = new NursingLevelVo();
            BeanUtils.copyProperties(pojo, nursingLevelVo);
            return nursingLevelVo;
        }).collect(Collectors.toList());
        return nursingLevelVos;
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> findByPage(int pageNum, int pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> p = nursingLevelMapper.findByPage(name, status);
        return PageResponse.of(p, NursingLevelVo.class);
    }

    /**
     * 添加护理等级
     * @param nursingLevelDto
     */
    @Override
    public void add(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);
        nursingLevelMapper.insert(nursingLevel);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo findById(Long id) {
        NursingLevelVo nursingLevelVo = nursingLevelMapper.selectById(id);
        return nursingLevelVo;
    }

    /**
     * 更新护理等级
     * @param nursingLevelDto
     */
    @Override
    @Transactional
    public void update(NursingLevelDto nursingLevelDto) {
        // 1.根据id查询出护理等级
        NursingLevelVo nursingLevelVo = nursingLevelMapper.selectById(nursingLevelDto.getId());
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelVo, nursingLevel);

        // 2.修改数据
        nursingLevel.setName(nursingLevelDto.getName());
        nursingLevel.setPlanId(nursingLevelDto.getPlanId());
        nursingLevel.setFee(nursingLevelDto.getFee());
        nursingLevel.setStatus(nursingLevelDto.getStatus());
        nursingLevel.setDescription(nursingLevelDto.getDescription());

        // 3.调用mapper更新
        nursingLevelMapper.update(nursingLevel);
    }

    /**
     * 删除护理等级
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingLevelMapper.delete(id);
    }

    @Override
    @Transactional
    public void updateStatus(Long id, Integer status) {
        // 1.根据id查询出护理等级
        NursingLevelVo nursingLevelVo = nursingLevelMapper.selectById(id);
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelVo, nursingLevel);

        // 2.修改数据
        nursingLevel.setStatus(status);

        // 3.调用mapper更新
        nursingLevelMapper.update(nursingLevel);
    }
}
